﻿using DevExpress.XtraEditors;
using DevTeam;

using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.GomlekCekiListesi
{
    public partial class GP : XIPL
    {
        public GP()
        {
            InitializeComponent();

            Init("GP Çeki Listesi", false);
            this.FileDone += DestexTip1_FileDone;
        }

        private void GP_Load(object sender, EventArgs e)
        {
            gleCKBatch.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            gleSezon.Visibility = txtOrderRef.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
        }

        void DestexTip1_FileDone(DataSet ds, string FileName)
        {
            int PlTableCount = 0;
            foreach (DataTable dt in ds.Tables)
                if (dt.TableName.StartsWith("PL"))
                {
                    PlTableCount++;

                    DataRowCollection rows = dt.Rows;
                    int orderLineStart = -1;

                    for (int i = 0; i < rows.Count && orderLineStart < 0; i++)
                    {
                        DataRow dr = rows[i];
                        switch (dr[0].ToStr())
                        {
                            case "codcli":
                                orderLineStart = i + 1;
                                break;
                        }
                    }

                    Dictionary<string, int> indexes = new Dictionary<string, int>();

                    DataRow dr0 = rows[orderLineStart - 1];
                    for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
                    {
                        switch (dr0[colIndex].ToStr())
                        {
                            case "codcli": indexes.Add("Magaza", colIndex); break;
                            case "mod_tex": indexes.Add("Modello", colIndex); break;
                            case "fattura": indexes.Add("FaturaNo", colIndex); break;
                            case "box": indexes.Add("BoxNo", colIndex); break;
                        }
                    }

                    //Bedenleri önce olduğu gibi sakla, sonra bütün sheet'leri birleştirerek DOsyaNo.ları ve Dağılımları bulmaya çalış, bulamadıklarını sor!

                    int sira = 0;
                    for (int c = indexes["BoxNo"] + 1; c < dt.Columns.Count; c++)
                    {
                        string beden = rows[orderLineStart - 1][c].ToStr();

                        DataRow r = dtBD.NewRow();
                        r["SheetName"] = dt.TableName;
                        r["Caption"] = beden;
                        r["Sira"] = ++sira;
                        dtBD.Rows.Add(r);
                    }

                    for (int i = orderLineStart; i < rows.Count; i++)
                    {
                        DataRow r = rows[i];
                        if (r[indexes["BoxNo"]].ToStr() != string.Empty)
                        {
                            DataRow drArticle = Article.NewRow();

                            drArticle["SheetName"] = dt.TableName;
                            drArticle["BoxNo"] = r[indexes["BoxNo"]].ToInt();
                            drArticle["Modello"] = r[indexes["Modello"]].ToStr();
                            drArticle["FaturaNo"] = r[indexes["FaturaNo"]].ToStr();
                            drArticle["Magaza"] = r[indexes["Magaza"]].ToStr();

                            for (int c = indexes["BoxNo"] + 1; c < dt.Columns.Count; c++)
                            {
                                //string beden = rows[orderLineStart - 1][c].ToStr();
                                int adet = r[c].ToInt();

                                if (adet > 0)
                                    drArticle["x" + (c - indexes["BoxNo"])] = adet;
                            }

                            Article.Rows.Add(drArticle);
                        }

                    }

                }

            if (PlTableCount == 0)
                XtraMessageBox.Show("Dosyada, adı PL ile başlayan sheet yok!");


            string str = Article.Serialize();
            dtBoth = ExecuteDataTable("ComparePL", 20, gleSezon.EditValue.ToInt(), txtOrderRef.EditValue.ToStr(), gleCKBatch.EditValue.ToInt(), str, string.Empty);

        }
    }
}